import { Application } from 'egg'
import { Models } from 'sequelize'

export default (app: Application) => {
  // const { UUID, UUIDV4, STRING, BOOLEAN, DATE, NOW, INTEGER, BIGINT, DOUBLE, JSONB, BLOB } = app.Sequelize.DataTypes
  const { UUID, UUIDV4, STRING, BOOLEAN, DATE, INTEGER, JSONB, BLOB } = app.Sequelize.DataTypes
  /** 用户 */
  const model = app.model.define('users', {
    id: { type: UUID, primaryKey: true, defaultValue: UUIDV4 },
    /** 用户名 登录名 */
    name: { type: STRING, allowNull: false, unique: true },
    /** 用户是否被锁定 */
    isLocked: BOOLEAN,
    /** 用户有效期 */
    expiredAt: DATE,
    /** 登陆次数 */
    loginCount: { type: INTEGER, defaultValue: 0 },
    /** 登陆时间 */
    loginAt: DATE,
    /** 登陆失败次数 */
    loginFailedCount: { type: INTEGER, defaultValue: 0 },
    /** hash后的密码 */
    password: STRING,
    /** 电子邮箱 */
    email: STRING,
    /** 手机号 */
    phone: STRING,
    /** 昵称 */
    nickName: STRING,
    /** 详细地址 */
    address: STRING,
    /** 用户头像 url */
    picture: STRING,
    /** 用户头像文件表id files.id */
    pictureFileId: UUID,
    // balance: DOUBLE,
    // autoNo: { type: BIGINT, autoIncrement: true, field: 'auto_no' },
    /** 测试用，用户扩展数据 */
    data: JSONB,
    /** 测试用 */
    pictureData: BLOB,
    // createdAt: { type: DATE, allowNull: false, defaultValue: NOW },
    // updatedAt: { type: DATE, allowNull: false, defaultValue: NOW },
    // deletedAt: { type: DATE },
    // version: { type: INTEGER, allowNull: false, defaultValue: 0 },
  })

  model.associate = (models: Models) => {
    model.hasMany(app.model['Setting'])
  }

  return model
}
